import supply from "../geojson/supply.json"
import materialWall from "../extent/materialWall"
import xtIcon from "../styles/img/xt.svg"
const SupplyManage = function(viewer, Cesium) {
    var self = this
    this.viewer = viewer
    this.Cesium = Cesium
    this.LabelHieght = 130
    this.WallHeight = 150
    this.currentEntity = null
}

SupplyManage.prototype = {
    init() {
        supply.features.forEach(featur => {
            var poins = []
            var heights = []
            let lpoints = []
            featur.geometry.coordinates[0].forEach(coor => {
                poins = poins.concat(coor)
                coor.push(350)
                heights.push(350)
                lpoints = lpoints.concat(coor)
            })
            let posct3 = this.Cesium.Cartesian3.fromDegreesArray(poins)
            let ringpos3 = this.Cesium.Cartesian3.fromDegreesArrayHeights(lpoints)
            var hpoint = this.Cesium.Cartesian3.fromDegrees(featur.properties.centerLon, featur.properties.centerLat, this.LabelHieght);
            var point = {
                key: "enclosure",
                position: hpoint,
                id: featur.properties.Name,
                show: false,
                wall: {
                    positions: posct3,
                    maximumHeights: heights,
                    // material:Color.fromRandom({ alpha: 0.7 })
                    material: new materialWall()
                },
                polyline: {
                    positions: ringpos3,
                    width: 5,
                    material:
                    //  new this.Cesium.PolylineTrailLinkMaterialProperty(new this.Cesium.Color(0.6, 0.6, 0.2, 0.9), 3000)
                        this.Cesium.Color.fromCssColorString('#ff0').withAlpha(1)
                },
                billboard: new this.Cesium.BillboardGraphics({
                    image: xtIcon,
                    eyeOffset: new this.Cesium.Cartesian3(0.0, 20.0, 0.0),
                    disableDepthTestDistance: Number.POSITIVE_INFINITY,
                    scaleByDistance: new this.Cesium.NearFarScalar(5000, 1, 200000, 0),
                    // heightReference:HeightReference.CLAMP_TO_GROUND,
                    pixelOffset: new this.Cesium.Cartesian2(20, -86),
                    scale: 0.5
                }),
                label: new this.Cesium.LabelGraphics({

                    text: "供电单元:" + featur.properties.Name,
                    font: '18px sans-serif',
                    fillColor: new this.Cesium.Color(1, 1, 1, 1),
                    outlineWidth: 2,
                    showBackground: true,
                    style: this.Cesium.LabelStyle.FILL_AND_OUTLINE,
                    eyeOffset: new this.Cesium.Cartesian3(0.0, 20.0, 0.0),
                    backgroundColor: new this.Cesium.Color.fromCssColorString('#333').withAlpha(0.7),
                    verticalOrigin: this.Cesium.VerticalOrigin.BOTTOM,
                    horizontalOrigin: this.Cesium.HorizontalOrigin.CENTER,
                    pixelOffset: new this.Cesium.Cartesian2(20, -148),
                    // scaleByDistance: new this.Cesium.NearFarScalar(18000, 1, 100000, 0),
                    disableDepthTestDistance: 2000000,
                    // heightReference:HeightReference.CLAMP_TO_GROUND
                }),
                attr: featur.properties
            }

            this.viewer.entities.add(point)
        })
    },
    hide() {
        if (this.currentEntity) {
            this.currentEntity.show = false
        }
    },
    show(id) {
        let cureentEntity = this.viewer.entities.getById(id)
        if (this.currentEntity) {
            this.currentEntity.show = false
        }
        if (cureentEntity) {
            cureentEntity.show = true
            this.currentEntity = cureentEntity
            var hpoint = this.Cesium.Cartesian3.fromDegrees(cureentEntity._attr.centerLon, cureentEntity._attr.centerLat - 0.028, 3000);
            this.viewer.camera.flyTo({
                destination: hpoint,
                orientation: {
                    heading: this.Cesium.Math.toRadians(0),
                    pitch: this.Cesium.Math.toRadians(-40),
                    roll: 0.0
                }
            })
        }
    },
    goto(id) {

    },
    guid() {
        function S4() {
            return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
        }

        return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
    }

}

export default SupplyManage